package com.marion.study.repo.write;

import com.marion.study.entity.ProductEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface ProductWriteRepo extends JpaRepository<ProductEntity, Integer> {

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    @Modifying
    @Query(value = "update t_product set inventory = inventory - :inventory  where id=:id", nativeQuery = true)
    void reduceInventory(@Param("id") long id, @Param("inventory") int inventory);

}
